<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>滚动动画</title>
  <style>
      body{
        background-color: skyblue;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        margin: 0;
        /* 隐藏x轴溢出的部分 */
        overflow-x: hidden;
      }
      h1{
        margin: 10px;
      }
      .box{
        background-color:#fff;
        color: aqua;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 400px;
        height: 200px;
        margin: 10px;
        border-radius: 75px;
        transform: translateX(400%);
        /* 让格子放在右边 */
        transition: transform 0.5s ease;
      }
      .box:nth-of-type(even){
        /* 让格子放在左边 */
        transform: translateX(-400%);
      }
      .box.show{
        transform: translateX(0);
      }
      .box h2{  
        font-size: 45px;
      }
  </style>
</head>
  <h1>滚动有效果</h1>
  <div class="box">
    <h2>↓</h2>
  </div>
  <div class="box">
    <h2>↓</h2>
  </div>
  <div class="box">
    <h2>↓</h2>
  </div>
  <div class="box">
    <h2>→</h2>
  </div>
  <div class="box">
    <h2>←</h2>
  </div>
  <div class="box">
    <h2>→</h2>
  </div>
  <div class="box">
    <h2>←</h2>
  </div>
  <div class="box">
    <h2>→</h2>
  </div>
  <div class="box">
    <h2>←</h2>
  </div>
  <div class="box">
    <h2>→</h2>
  </div>
  <div class="box">
    <h2>←</h2>
  </div>
  <div class="box">
    <h2>→</h2>
  </div>
  <script>
    const boxes = document.querySelectorAll('.box');
    window.addEventListener('scroll', checkBoxes);
    checkBoxes();
    function checkBoxes(){
      console.log(window.innerHeight);
      boxes.forEach((box) => {
        let triggerHeight = (window.innerHeight * 2) / 3;
        let boxTop = box.getBoundingClientRect().top;
        if (boxTop < triggerHeight) {
          box.classList.add('show');
        } else {
          box.classList.remove('show');
        }
      });
    }

  </script>
</body>
</html>